from tkinter import *
import re


def validate_input():
    reg = '^[+-]?[\d]+[.]?[\d]+$'
    r1 = re.match(reg, op1.get())
    r2 = re.match(reg, op2.get())
    return r1 and r2


def add_cmd1():
    if validate_input():
        op1_value = float(op1.get())
        op2_value = float(op2.get())
        res_var.set(round(op1_value + op2_value, 2))
    else:
        res_var.set('请输入正确的操作数')


def add_cmd():
    try:
        op1_value = int(op1.get())
        op2_value = int(op2.get())
        res_var.set(op1_value + op2_value)
    except ValueError:
        res_var.set('请输入正确的操作数')


def clear():
    op1.delete(0, END)
    op2.delete(0, END)


def calc_art():
    ex = op_art.get()
    res = eval(ex)
    res_str.set(res)


# 创建一个根窗口
root = Tk()

menubar = Menu(root)
menu1 = Menu(menubar)
menu1.add_command(label='普通', command=lambda: f1.tkraise())
menu1.add_command(label='文艺', command=lambda: f2.tkraise())
menubar.add_cascade(label='模式', menu=menu1)
root.config(menu=menubar)

# -------------------------------------------------------
f1 = Frame(root)
f1.grid(row=0, column=0, sticky='news')

op1 = Entry(f1)
op2 = Entry(f1)

btn_add = Button(f1, text='+', padx=50, pady=10, command=add_cmd1)
btn_sub = Button(f1, text='-', padx=50, pady=10)
btn_mul = Button(f1, text='*', padx=50, pady=10)
btn_div = Button(f1, text='/', padx=50, pady=10)
btn_mod = Button(f1, text='%', padx=50, pady=10)
btn_flo = Button(f1, text='//', padx=50, pady=10)
btn_exp = Button(f1, text='**', padx=50, pady=10)
btn_clr = Button(f1, text='Clear', padx=50, pady=10, command=clear)

res_var = StringVar()
res_var.set('Show Result')
result = Label(f1, textvariable=res_var, pady=30)

op1.grid(row=0, column=0, columnspan=4, sticky='WE')
op2.grid(row=1, column=0, columnspan=4, sticky='WE')
btn_add.grid(row=2, column=0, sticky='WE')
btn_sub.grid(row=2, column=1, sticky='WE')
btn_mul.grid(row=2, column=2, sticky='WE')
btn_div.grid(row=2, column=3, sticky='WE')
btn_mod.grid(row=3, column=0, sticky='WE')
btn_flo.grid(row=3, column=1, sticky='WE')
btn_exp.grid(row=3, column=2, sticky='WE')
btn_clr.grid(row=3, column=3, sticky='WE')
result.grid(row=4, column=0, columnspan=4, sticky='WE')
# ------------------------------------------------------------
f2 = Frame(root)
f2.grid(row=0, column=0, sticky='news')

op_art = Entry(f2)
btn_art = Button(f2, text='计算', command=calc_art)
res_str = StringVar()
res_str.set('计算结果显示在这里')
label_art = Label(f2, textvariable=res_str)

op_art.pack(fill=BOTH)
btn_art.pack(fill=BOTH)
label_art.pack(fill=BOTH)
# root.geometry('500x500+200+200')

mainloop()
